<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h2{
            width: 400px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            margin: 20px auto;
        }
        #box{
            width: 400px;
            height: 400px;
            border: 1px solid black;
            margin: 0 auto;
            position: relative;
        }
        #btn{
            display: block;
            width: 100px;
            height: 30px;
            margin: 20px auto;
        }
        #box .cross {
            position: absolute;
        }
        #box .cross div{
            width: 100px;
            height: 100px;
            float: left;
            background: blanchedalmond;
        }
        #box .cross .black{
            background: black;
        }
    </style>
    <script src="../util.js"></script>
</head>
<body>
    <h2>0</h2>
    <div id="box">
        
    </div>
    <button id="btn">开始</button>
    <script>
        window.onload = function () {
            var box = document.querySelector('#box')
            var btn = document.querySelector('#btn')
            
            var index = 0
            var n = 0
            setInterval(() =>{
                
                var len = index++
                var cdiv = document.createElement('div')
                cdiv.className = 'cross'
                for(var i=0;i<4;i++){
                    var bdiv = document.createElement('div')
                    cdiv.appendChild(bdiv)
                }
                box.appendChild(cdiv)
                var x = Math.floor(Math.random()*4)
                var bdiv = cdiv.querySelectorAll('div')
                var height = bdiv[0].offsetHeight
                cdiv.style.top = n*height + 'px'
                n++
                if(n>3){
                    n = 0
                }
                bdiv[x].classList.add('black') 
                if(index>4){
                    var c = box.querySelectorAll('.cross')
                    c[3].remove()
                }  
            },1000)
        }
    </script>
</body>
</html>